<?php


namespace app\api\model\User;


use think\facade\Db;
use think\Model;

class UserVipCodeModel extends Model
{

    /**
     * 数据表名
     * @var string
     */
    protected $name = "user_vip_code";

    /**
     * 根据条件查询单条数据
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getUserVipCodeWhere($where)
    {
        $code = Db::name("user_vip_code")->where($where)->order('id desc')->find();
        return $code;
    }

    /**
     * 根据条件查询多条数据
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getUserVipCodeWhereMore($where,$filed="*")
    {
        $list = Db::name("user_vip_code")->field($filed)->where($where)->select()->toArray();
        return $list;
    }

    /**
     * 统计总数
     * @param $where
     * @return int
     */
    public static function getUserVipCodeCountWhere($where)
    {
        $count = Db::name("user_vip_code")
            ->alias('uvc')
            ->join('user u','u.id = uvc.uid','left')
            ->where($where)->count();
        return $count;
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getUserVipCodeListWhere($page,$limit,$where = [],$field="uvc.*")
    {
        $start = ceil(($page-1)*$limit);
        $list = Db::name('user_vip_code')
            ->alias('uvc')
            ->join('user u','u.id = uvc.uid','left')
            ->field($field)
            ->where($where)
            ->limit($start,$limit)
            ->select()->toArray();
        return $list;
    }

    /**
     * 添加返回ID
     * @param $data
     * @return int
     */
    public static function addUserVipCode($data)
    {
        $id = Db::name('user_vip_code')->insertGetId($data);
        return $id;
    }

    /**
     * 批量添加
     * @param $data
     * @return int
     */
    public static function addUserVipCodeMore($data)
    {
        $flag = Db::name('user_vip_code')->insertAll($data);
        return $flag;
    }

    /**
     * 更新
     * @param $where
     * @param $save
     * @return int
     * @throws
     */
    public static function updateUserVipCodeWhere($where,$save)
    {
        $flag = Db::name('user_vip_code')->where($where)->update($save);
        return $flag;
    }

    /**
     * 删除
     * @param $where
     * @return int
     * @throws
     */
    public static function delUserVipCodeWhere($where)
    {
        $flag = Db::name('user_vip_code')->where($where)->delete();
        return $flag;
    }

}